package com.comac.print.component.app.exception;

import cn.hutool.core.util.StrUtil;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.extension.api.R;
import com.comac.print.component.app.common.exception.PrintCommonException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author wfb
 */
@RestControllerAdvice
public class ExceptionsHandler {
    /**
     * 业务异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(BizException.class)
    public R bizExceptionHandler(BizException ex) {
        return R.failed(ex.getMessage());
    }

    @ExceptionHandler(PrintCommonException.class)
    public Response printCommonExceptionHandler(PrintCommonException ex) {
        return Response.buildFailure(
                StrUtil.isBlank(ex.getErrCode()) ? "-1" : ex.getErrCode(),
                ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Response exceptionHandler(Exception ex) {
        return Response.buildFailure("-1",
                ex.getMessage());
    }


    /**
     * 参数转换异常拦截
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public R exceptionHandler(HttpMessageNotReadableException ex) {
        return R.failed("参数格式异常！");
    }
}
